(function() {
  var express, iced, pgsql, router, secure, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  secure = require('../lib/secure');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/secure.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 12
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/secure.coffee"
          });
          client.query("select * from pay_secure limit 1", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 16
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length === 0) {
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/secure.coffee"
              });
              client.query("insert into pay_secure ( uuid, mpos_skey1, mpos_skey2, notes ) values ($1, $2, $3, $4)", [uuid.v4(), '', '', '默认配置'], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 26
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              done();
              return __iced_k(res.redirect('/secure'));
            });
          } else {
            done();
            return __iced_k(res.render('secure', {
              record: result.rows[0]
            }));
          }
        });
      };
    })(this));
  });

  router.post('/edit', function(req, res, next) {
    var client, done, err, k, mpos_skey1, mpos_skey2, old_record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['uuid', 'mpos_skey1', 'mpos_skey2', 't40_max_amt'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    mpos_skey1 = secure.encryptEde(req.body.mpos_skey1);
    mpos_skey2 = secure.encryptEde(req.body.mpos_skey2);
    if (!mpos_skey1 || !mpos_skey2) {
      return next(new Error('加密母POS密钥分量错误，请检查...'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/secure.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 44
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/secure.coffee"
          });
          client.query("select * from pay_secure where uuid = $1", [req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 50
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length !== 1) {
            done();
            return new Error('查询安全信息错误');
          }
          old_record = result.rows[0];
          if (old_record.mpos_skey1 !== req.body.mpos_skey1) {
            req.body.mpos_skey1 = secure.encryptEde(req.body.mpos_skey1);
            if (!req.body.mpos_skey1) {
              done();
              return next(new Error('加密母 POS 密钥分量1 错误，请检查...'));
            }
          }
          if (old_record.mpos_skey2 !== req.body.mpos_skey2) {
            req.body.mpos_skey2 = secure.encryptEde(req.body.mpos_skey2);
            if (!req.body.mpos_skey2) {
              done();
              return next(new Error('加密母 POS 密钥分量2 错误，请检查...'));
            }
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/secure.coffee"
            });
            client.query("update pay_secure set mpos_skey1 = $1, mpos_skey2 = $2, t40_max_amt = $3, notes = $4 where uuid = $5", [req.body.mpos_skey1, req.body.mpos_skey2, req.body.t40_max_amt, req.body.notes, req.body.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 81
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.redirect('/secure');
          });
        });
      };
    })(this));
  });

  router.post('/encrypt_ede', function(req, res, next) {
    var k, key, _i, _len, _ref;
    res.type('json');
    _ref = ['key'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    if (!req.session.userinfo.view_keys) {
      return next(new Error("当前用户没有访问机密信息的权限"));
    }
    key = secure.encryptEde(req.body.key);
    if (!key) {
      return next(new Error('加密失败，请查看日志'));
    }
    return res.json({
      succ: true,
      key: key
    });
  });

  router.post('/decrypt_ede', function(req, res, next) {
    var k, key, _i, _len, _ref;
    res.type('json');
    _ref = ['key'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    if (!req.session.userinfo.view_keys) {
      return next(new Error("当前用户没有访问机密信息的权限"));
    }
    key = secure.decryptEde(req.body.key);
    if (!key) {
      return next(new Error('加密失败，请查看日志'));
    }
    return res.json({
      succ: true,
      key: key
    });
  });

}).call(this);
